package com.mogujie.tt.customview.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.mogujie.tt.R;

/**
 * Created by user on 2015/9/23.
 */

public class PhoneVerificationCode extends View
{
    /**
     * 文本
     */
    private String mTitleText;
    /**
     * 文本的颜色
     */
    private int mTitleTextColor;
    /**
     * 文本的大小
     */
    private int mTitleTextSize;

    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect mBound;
    private Paint mPaint;

    private PhoneVerificationCode mPhoneVerificationCode;
    private Thread pouringThread;
    private boolean interceptFlag = false;
    private static final String mAddSuffix ="s后获取";

    /*手机验证码60s延时*/
    private static final int ON_POURING = 1;
    private static final int POURING_FINISH = 2;
    private int timeOut = 60;

    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ON_POURING://正在倒时...
                    mTitleText = String.valueOf(timeOut)+mAddSuffix;
                    postInvalidate();
                    break;
                case POURING_FINISH://倒时完成
                    timeOut =60;
                    mPhoneVerificationCode.setClickable(true);
                    mTitleTextColor = Color.rgb(32,230,44);
                    mTitleText = "获取验证码";
                    postInvalidate();//强制重绘
                    break;
                default:
                    break;
            }
        };
    };

    private Runnable mdownApkRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                do{
                    Thread.sleep(1000);
                    timeOut--;
                    mHandler.sendEmptyMessage(ON_POURING);//发送空消息
                    if(timeOut == 0){
                        mHandler.sendEmptyMessage(POURING_FINISH);//发送空消息
                        interceptFlag =true;
                    }
                }while(!interceptFlag);

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };




    public PhoneVerificationCode(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public PhoneVerificationCode(Context context)
    {
        this(context, null);
    }

    /**
     * 获得我自定义的样式属性
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public PhoneVerificationCode(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        /**
         * 获得我们所定义的自定义样式属性
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PhoneVerificationCode, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++)
        {
            int attr = a.getIndex(i);
            switch (attr)
            {
                case R.styleable.PhoneVerificationCode_titleText:
                    mTitleText = a.getString(attr);
                    break;
                case R.styleable.PhoneVerificationCode_TextColor:
                    // 默认颜色设置为黑色
                    mTitleTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.PhoneVerificationCode_titleTextSize:
                    // 默认设置为16sp，TypeValue也可以把sp转化为px
                    mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;

            }

        }
        a.recycle();

        mPhoneVerificationCode = this;
        /**
         * 获得绘制文本的宽和高
         */
        mPaint = new Paint();
        mPaint.setTextSize(mTitleTextSize);
        mPaint.setColor(mTitleTextColor);
        mBound = new Rect();
        mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);//设置宽度

/*		this.setOnClickListener(new ClickCustomListener()
		{

			@Override
			public void onClick(View v) {
				super.onClick(v);

			}


		});*/

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = 0;
        int height = 0;

        /**
         * 设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        switch (specMode)
        {
            case MeasureSpec.EXACTLY:// 明确指定了
                width = getPaddingLeft() + getPaddingRight() + specSize;
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                width = getPaddingLeft() + getPaddingRight() + mBound.width();
                break;
        }

        /**
         * 设置高度
         */
        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (specMode)
        {
            case MeasureSpec.EXACTLY:// 明确指定了
                height = getPaddingTop() + getPaddingBottom() + specSize;
                break;
            case MeasureSpec.AT_MOST:// 一般为WARP_CONTENT
                height = getPaddingTop() + getPaddingBottom() + mBound.height();
                break;
        }

        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        //画整个区域
        mPaint.setColor(Color.WHITE);//白色
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

        //画字符
        mPaint.setColor(mTitleTextColor);
        canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
    }

    /**
     * 方式
     */
    public void  methods() {
        mTitleTextColor = Color.GRAY;//设置为灰色
        interceptFlag =false;
        mPhoneVerificationCode.setClickable(false);
        pouringThread = new Thread(mdownApkRunnable);
        pouringThread.start();
    }
}